from django.http import JsonResponse
from rest_framework.response import Response
from rest_framework import status

class APIResponse:
    @staticmethod
    def success(data=None, message="操作成功", status=200):
        """
        成功响应
        :param data: 响应数据
        :param message: 响应消息
        :param status: HTTP状态码
        :return: JsonResponse
        """
        return JsonResponse({
            'code': 1,
            'msg': message,
            'data': data
        }, status=status)

    @staticmethod
    def error(message="操作失败", status=400, data=None):
        """
        错误响应
        :param message: 错误消息
        :param status: HTTP状态码
        :param data: 错误详情数据
        :return: JsonResponse
        """
        return JsonResponse({
            'code': 0,
            'msg': message,
            'data': data
        }, status=status)

    @staticmethod
    def rest_success(data=None, message="操作成功", status=status.HTTP_200_OK):
        """
        DRF成功响应
        :param data: 响应数据
        :param message: 响应消息
        :param status: HTTP状态码
        :return: Response
        """
        return Response({
            'code': 1,
            'msg': message,
            'data': data
        }, status=status)

    @staticmethod
    def rest_error(message="操作失败", status=status.HTTP_400_BAD_REQUEST, data=None):
        """
        DRF错误响应
        :param message: 错误消息
        :param status: HTTP状态码
        :param data: 错误详情数据
        :return: Response
        """
        return Response({
            'code': 0,
            'msg': message,
            'data': data
        }, status=status)